<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 充值流水模型
 * @package app\admin\model
 */
class RecordM extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_record';

    public function searchOffRedisAttr($query, $value, $data)
    {
        if(isMmsOrSms()){
            $query->where('bus_channel.off_redis','=', 0);
        }
        
    }

    public function getRecordAll($request)
    {

    	$data = $this->withSearch(['off_redis'])
    		->alias('r')
    		->field('r.*,bus_channel.company_name,u.username,o.ope_type')
            ->join('adm_user u','u.id=r.operation_user','LEFT')
            ->join('main_record_ope o','o.id=r.operation_type','LEFT')
    		->join('bus_channel','bus_channel.channel_id=r.channel_id','LEFT');

        switch (session('user_type')) {

            case 'admin':

                break;

            case 'channel':
                if(is_OneCha()) {

                    $data = $data->where(function ($query) {
                            $query->where('bus_channel.p_id', session('relate_id'))->whereor('r.channel_id', session('relate_id'));
                        });

                } else {

                    $data = $data->where('r.channel_id', session('relate_id'));
                }
                break;
            
            default:

                if(session('username') == 'admin2018') {

                    if(!empty($request['channel_id'])) {

                        $data = $data->where('r.channel_id', $request['channel_id']);
                    }
                } else {

                    return;
                }
                break;
        }

        if(!empty($request['channel_id'])) {

            $data = $data->where('r.channel_id', $request['channel_id']);
        }

        $data = $data->whereTime('r.operation_time', 'between', [ $request['st'], $request['et'] ]);

        if(!empty($request['oty'])) {

            $data = $data->where('r.operation_type',$request['oty']);
        }

    	$data = $data->order('r.operation_time desc')->paginate($request['limit'], false, ['query'=>$request]);
    	return $data;
    }

}